﻿using System.Text.RegularExpressions;

namespace Nasa8x.Core
{
   public static class BBCodeToHtml
    {
       private const RegexOptions _options = RegexOptions.Singleline | RegexOptions.IgnoreCase;

        /// <summary>
        /// A method to convert basic BBCode to HTML
        /// </summary>
        /// <param name="str">A string formatted in BBCode</param>
        /// <returns>The HTML representation of the BBCode string</returns>
       public static string Convert(string str)
       {
            
           // format the bold tags: [b][/b]
           // becomes: <strong></strong>
           Regex exp = new Regex(@"\[b\](.+?)\[/b\]",_options);
           str = exp.Replace(str, "<strong>$1</strong>");

           // format the italic tags: [i][/i]
           // becomes: <em></em>
           exp = new Regex(@"\[i\](.+?)\[/i\]");
           str = exp.Replace(str, "<em>$1</em>");

           // format the underline tags: [u][/u]
           // becomes: <u></u>
           exp = new Regex(@"\[u\](.+?)\[/u\]");
           str = exp.Replace(str, "<u>$1</u>");


           // format the underline tags: [left][/left]
           // becomes: <p align="left"><p>
           exp = new Regex(@"\[left\](.+?)\[/left\]");
           str = exp.Replace(str, "<p align=\"left\">$1</p>");

           // format the underline tags: [center][/center]
           // becomes: <p align="center"><p>
           exp = new Regex(@"\[center\](.+?)\[/center\]");
           str = exp.Replace(str, "<p align=\"center\">$1</p>");

           // format the underline tags: [right][/right]
           // becomes: <p align="right"><p>
           exp = new Regex(@"\[right\](.+?)\[/right\]");
           str = exp.Replace(str, "<p align=\"right\">$1</p>");

           // format the underline tags: [justify][/justify]
           // becomes: <p align="right"><p>
           exp = new Regex(@"\[justify\](.+?)\[/justify\]");
           str = exp.Replace(str, "<p align=\"justify\">$1</p>");

           // format the strike tags: [s][/s]
           // becomes: <strike></strike>
           exp = new Regex(@"\[s\](.+?)\[/s\]");
           str = exp.Replace(str, "<strike>$1</strike>");

           // format the strike tags: [strikethrough][/strikethrough]
           // becomes: <strike></strike>
           exp = new Regex(@"\[strikethrough\](.+?)\[/strikethrough\]");
           str = exp.Replace(str, "<strike>$1</strike>");


           // format the strike tags: [quote=admin][/quote]

           while (Regex.IsMatch(str, @"\[quote=(.*?)\](.*)\[/quote\]", _options))
               str = Regex.Replace(str, @"\[quote=(.*?)\](.*)\[/quote\]", "<div class=\"quote\"><div class=\"quote-poster\">$1:</div><div class=\"quote-content\">$2</div></div>", _options);

           while (Regex.IsMatch(str, @"\[quote\](.*)\[/quote\]", _options))
               str = Regex.Replace(str, @"\[quote\](.*)\[/quote\]", "<div class=\"quote\"><div class=\"quote-content\">$1</div></div>", _options);
           //exp = new Regex(@"\[quote=(.*?)\](.*)\[/quote\]", _option);
           //str = exp.Replace(str, "<div class=\"quote\"><div class=\"quote-poster\">$1:</div><div class=\"quote-content\">$2</div></div>");

           // format the url tags: [url=www.website.com]my site[/url]
           // becomes: <a href="www.website.com">my site</a>
           exp = new Regex(@"\[url\=([^\]]+)\]([^\]]+)\[/url\]",_options);
           str = exp.Replace(str, "<a href=\"$1\" target=\"_blank\">$2</a>");

           // format the img tags: [img]www.website.com/img/image.jpeg[/img]
           // becomes: <img src="www.website.com/img/image.jpeg" />
           exp = new Regex(@"\[img\]([^\]]+)\[/img\]");
           str = exp.Replace(str, "<img src=\"$1\" />");

           // format img tags with alt: [img=www.website.com/img/image.jpeg]this is the alt text[/img]
           // becomes: <img src="www.website.com/img/image.jpeg" alt="this is the alt text" />
           exp = new Regex(@"\[img\=([^\]]+)\]([^\]]+)\[/img\]");
           str = exp.Replace(str, "<img src=\"$1\" alt=\"$2\" />");

           //format the colour tags: [color=red][/color]
           // becomes: <font color="red"></font>
           // supports UK English and US English spelling of colour/color
           exp = new Regex(@"\[color\=([^\]]+)\]([^\]]+)\[/color\]");
           str = exp.Replace(str, "<font color=\"$1\">$2</font>");
           exp = new Regex(@"\[colour\=([^\]]+)\]([^\]]+)\[/colour\]");
           str = exp.Replace(str, "<font color=\"$1\">$2</font>");

           // format the size tags: [size=3][/size]
           // becomes: <font size="+3"></font>
           exp = new Regex(@"\[size\=([^\]]+)\]([^\]]+)\[/size\]");
           str = exp.Replace(str, "<font size=\"+$1\">$2</font>");


           //while (Regex.IsMatch(str, @"<.*?>", _options))
           //    str = Regex.Replace(str, @"<.*?>", "", _options);
           //while (Regex.IsMatch(str, @"\[.*?\]", _options))
           //    str = Regex.Replace(str, @"\[.*?\]", "", _options);


           // lastly, replace any new line characters with <br />
           str = str.Replace("\r\n", "<br />");
           str = str.Replace("\n", "<br />");
           str = str.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");

           return str;

       }
    }
}
